package org.dei.perla.aggregates;

import java.util.List;

import org.dei.perla.parser.expressions.ConstantFloat;

public class SampleVariance implements AggregationOperator {

	@Override
	public ConstantFloat doOperation(ConstantFloat[] sample) {
		double sumSquare = 0;
		double variance = 0;
		int len = sample.length;
		double mean = 0;
		mean = new SampleMean().doOperation(sample).getValueFloat();
		sumSquare = new SampleSumSquare().doOperation(sample).getValueFloat();
		variance = sumSquare/len - mean * mean;
		return new ConstantFloat(variance);
	}

	@Override
	public ConstantFloat doOperation(List<ConstantFloat> sample) {
		double sumSquare = 0;
    	double variance = 0;
    	double mean = 0;
        int len = sample.size();
        mean = new SampleMean().doOperation(sample).getValueFloat();
        sumSquare = new SampleSumSquare().doOperation(sample).getValueFloat();
		variance = sumSquare/len - mean * mean;
        return new ConstantFloat(variance);
	}

}
